import {
  HTTP
} from '../utils/http.js'
class ClassicModel extends HTTP {
  getLast(callback) {
    // 通过继承的方式直接使用this.request即可
    this.request({
      url: 'classic/latest',
      success: (res) => {
        callback(res);
        // 写入缓存(设置最新一期的ID号)
        this._setLastIndex(res.index);
        let key = this._getKey(res.index)
        wx.setStorageSync(key, res)
      }
    })
  }
  // 获取前一期
  getRight(index, callback) {
    let key = this._getKey(index - 1)
    let classic = wx.getStorageSync(key)
    if (!classic) {
      this.request({
        url: 'classic/' + index + '/previous',
        success: (res) => {
          wx.setStorageSync(this._getKey(res.index), res)
          callback(res);
        }
      })
    } else {
      callback(classic)
    }
  }
  // 获取下一期
  getLeft(index, callback) {
    let key = this._getKey(index + 1)
    let classic = wx.getStorageSync(key)
    if (!classic) {
      this.request({
        url: 'classic/' + index + '/next',
        success: (res) => {
          wx.setStorageSync(this._getKey(res.index), res)
          callback(res);
        }
      })
    } else {
      callback(classic)
    }
  }

  // 判断当前期刊是否第一期
  isFirst(index) {
    return index == 1 ? true : false
  }
  // 判断是否为最新一期(通过classic.js的getlatest方法拿到的数据就是最新一期)
  isLatest(index) {
    let laststIndex = this._getLastIndex();
    return laststIndex == index ? true : false
  }
  _setLastIndex(index) {
    wx.setStorageSync('latest', index)
  }
  _getLastIndex() {
    return wx.getStorageSync('latest')
  }
  // 缓存
  _getKey(index) {
    let key = 'classic-' + index;
    return key
  }
  getMyFavor(success){
    const params={
      url:'classic/favor',
      success:success
    }
    this.request(params)
  }
}
export {
  ClassicModel
}